package com.danawa.common;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class EntryPointRegistrar implements Registrar {

	private Map<Class<?>, Map<String, EntryPoint>> entryPoints;

	public void init() {
		entryPoints = new HashMap<Class<?>, Map<String, EntryPoint>>();
	}
	
	public void add(Class<?> entryPointClass, EntryPoint entryPoint) {
		Map<String, EntryPoint> theEntryPoint = entryPoints.get(entryPointClass);
		if (theEntryPoint == null) {
			theEntryPoint = new HashMap<String, EntryPoint>();
			entryPoints.put(entryPointClass, theEntryPoint);
		}
		
		theEntryPoint.put(entryPoint.getIdentity(), entryPoint);
	}

	public EntryPoint get(Class<?> entryPointClass, String identity) {
		Map<String, EntryPoint> theEntryPoint = entryPoints.get(entryPointClass);
		return theEntryPoint.get(identity);
	}
	
	@SuppressWarnings("unchecked")
	public Collection<? extends EntryPoint> getAll(Class<?> entryPointClass) {
		Map<String, EntryPoint> foundEntryPoints = entryPoints.get(entryPointClass);
		
		return (Collection<? extends EntryPoint>)
				Collections.unmodifiableCollection(foundEntryPoints != null ?
						foundEntryPoints.values() : Collections.EMPTY_SET);
	}
	
	public EntryPoint delete(Class<?> entryPointClass, String identity) {
		Map<String, EntryPoint> theEntryPoint = entryPoints.get(entryPointClass);
		return theEntryPoint.remove(identity);
	}
}
